package com.hycreate.platform.modules.product.element;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.modules.product.element.SfProductElement;
import com.hycreate.platform.modules.product.element.SfProductElementService;

/**
 * 产品配件关系Controller
 * @author hoader
 * @version 2017-01-09
 */
@Controller
@RequestMapping(value = "${adminPath}/product/element/sfProductElement")
public class SfProductElementController extends BaseController {

	@Autowired
	private SfProductElementService sfProductElementService;
	
	@ModelAttribute
	public SfProductElement get(@RequestParam(required=false) String id) {
		SfProductElement entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sfProductElementService.get(id);
		}
		if (entity == null){
			entity = new SfProductElement();
		}
		return entity;
	}
	
	@RequiresPermissions("product:element:sfProductElement:view")
	@RequestMapping(value = {"list", ""})
	public String list(SfProductElement sfProductElement, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SfProductElement> page = sfProductElementService.findPage(new Page<SfProductElement>(request, response), sfProductElement); 
		model.addAttribute("page", page);
		return "modules/product/element/sfProductElementList";
	}

	@RequiresPermissions("product:element:sfProductElement:view")
	@RequestMapping(value = "form")
	public String form(SfProductElement sfProductElement, Model model) {
		model.addAttribute("sfProductElement", sfProductElement);
		return "modules/product/element/sfProductElementForm";
	}

	@RequiresPermissions("product:element:sfProductElement:edit")
	@RequestMapping(value = "save")
	public String save(SfProductElement sfProductElement, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sfProductElement)){
			return form(sfProductElement, model);
		}
		sfProductElementService.save(sfProductElement);
		addMessage(redirectAttributes, "保存产品配件关系成功");
		return "redirect:"+Global.getAdminPath()+"/product/element/sfProductElement/?repage";
	}
	
	@RequiresPermissions("product:element:sfProductElement:edit")
	@RequestMapping(value = "delete")
	public String delete(SfProductElement sfProductElement, RedirectAttributes redirectAttributes) {
		sfProductElementService.delete(sfProductElement);
		addMessage(redirectAttributes, "删除产品配件关系成功");
		return "redirect:"+Global.getAdminPath()+"/product/element/sfProductElement/?repage";
	}

}